import websocketIo from "../until/websocket.io";
class moduleList {
    static timer;
    public while_id:number = 0;
    public moduleNum:number = 6;
    constructor(moduleNum:number) {
        this.moduleNum = moduleNum;
        this.startWhile()
    }
    // 开启轮询
    startWhile() {
        moduleList.timer = setInterval(() => {
            if (this.while_id === this.moduleNum) {
                this.while_id = 0
            }
            this.while_id ++
            let result = this.createData(this.while_id)
            if (websocketIo.getInstance.socketObj) websocketIo.getInstance.socketObj.send(JSON.stringify(result))
        },500)
    }
    // 创建一条数据
    createData(id:number) {
       let temp = this.randomValue(30,50)
       let hum = this.randomValue(20,40)
       let uvc = this.randomValue(30,100)
       let pre = this.randomValue(100,200)
       let inf = this.randomValue(20,30)

        let index = Math.floor(Math.random()*2)

       return  {"ID":id,data:[uvc,inf,temp,hum,pre],sta:[1,0,0,0,0]}
    }
    // 随机生成一个范围的数据
    randomValue(min:number,max:number) {
        return Math.floor(Math.random()*(max - min + 1) + min)
    }
}

export default new moduleList(1)
